<?php

/**
 * This is the model class for table "{{hzxx}}".
 *
 * The followings are the available columns in table '{{hzxx}}':
 * @property integer $id
 * @property string $usercode
 * @property string $username
 * @property string $userID
 * @property string $helpID
 * @property string $sex
 * @property integer $age
 * @property string $relation
 * @property string $helptype
 * @property string $usertype
 * @property string $monthMoney
 * @property string $email
 * @property string $region1
 * @property string $region2
 * @property string $region3
 * @property string $region4
 * @property string $region5
 * @property string $hometown
 * @property string $address
 * @property string $others
 * @property integer $tmpi1
 * @property integer $tmpi2
 * @property integer $tmpi3
 * @property string $tmps1
 * @property string $tmps2
 * @property string $tmps3
 */
class Hzxx extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{hzxx}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array(' usercode, username, userID, helpID, age, relation, helptype, usertype, monthMoney,   address', 'required'),
			array('id, age, tmpi1, tmpi2, tmpi3', 'numerical', 'integerOnly'=>true),
			array('usercode, username, userID, helpID, sex, relation, helptype, usertype, email, region1, region2, region3, region4, region5, hometown, address', 'length', 'max'=>20),
			array('monthMoney', 'length', 'max'=>18),
			array('others, tmps1, tmps2, tmps3', 'length', 'max'=>50),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
            array('region1','default','value'=>'辽宁省'),
            array('region2','default','value'=>'沈阳市'),
            array('region3','default','value'=>YII::app()->user->uuid),
            array('region4','default','value'=>YII::app()->user->facebook),
            array('region5','default','value'=>YII::app()->user->twitter),
			array('id, usercode, username, userID, helpID, sex, age, relation, helptype, usertype, monthMoney, email, region1, region2, region3, region4, region5, hometown, address, others, tmpi1, tmpi2, tmpi3, tmps1, tmps2, tmps3', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
            'famliys'=>array(self::HAS_MANY,'Famliyinfo','','on'=>'userID=userid'),
            'helpinfos'=>array(self::HAS_MANY,'Helpinfo','','on'=>'userID=userid'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'usercode' => '户主编码',
			'username' => '户主名称',
			'userID' => '户主身份证',
			'helpID' => '救助证号',
			'sex' => '性别',
			'age' => '年龄',
			'relation' => '联系方式',
			'helptype' => '社会救助类别',
			'usertype' => '人口分类',
			'monthMoney' => '月收入',
			'email' => 'Email',
			'region1' => '省',
			'region2' => '市',
			'region3' => '区',
			'region4' => '街道',
			'region5' => '社区',
			'hometown' => '籍贯',
			'address' => '现住地址',
			'others' => 'Others',
			'tmpi1' => 'Tmpi1',
			'tmpi2' => 'Tmpi2',
			'tmpi3' => 'Tmpi3',
			'tmps1' => 'Tmps1',
			'tmps2' => 'Tmps2',
			'tmps3' => 'Tmps3',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('usercode',$this->usercode,true);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('userID',$this->userID,true);
		$criteria->compare('helpID',$this->helpID,true);
		$criteria->compare('sex',$this->sex,true);
		$criteria->compare('age',$this->age);
		$criteria->compare('relation',$this->relation,true);
		$criteria->compare('helptype',$this->helptype,true);
		$criteria->compare('usertype',$this->usertype,true);
		$criteria->compare('monthMoney',$this->monthMoney,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('region1',$this->region1,true);
		$criteria->compare('region2',$this->region2,true);
		/** */
        $criteria->compare('region3',$this->region3,true);
  		$criteria->compare('region4',$this->region4,true);
  		$criteria->compare('region5',$this->region5,true);

        $criteria->addSearchCondition('region3',YII::app()->user->uuid);
        $criteria->addSearchCondition('region4',YII::app()->user->facebook);
        $criteria->addSearchCondition('region5',YII::app()->user->twitter);
        
		$criteria->compare('hometown',$this->hometown,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('others',$this->others,true);
		$criteria->compare('tmpi1',$this->tmpi1);
		$criteria->compare('tmpi2',$this->tmpi2);
		$criteria->compare('tmpi3',$this->tmpi3);
		$criteria->compare('tmps1',$this->tmps1,true);
		$criteria->compare('tmps2',$this->tmps2,true);
		$criteria->compare('tmps3',$this->tmps3,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Hzxx the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
